#include "allnetscanwidget.h"
#include "ui_allnetscanwidget.h"
#include "widget.h"
#include "protocol.h"
#include "Core/tcpclient.h"
#include "friendrequestwidget.h"
#include "Core/messagedispatcher.h"
AllNetScanWidget::AllNetScanWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AllNetScanWidget)
{
    ui->setupUi(this);
    LinkTcpSignalAndSlot();
}

AllNetScanWidget::~AllNetScanWidget()
{
    delete ui;
}

QListWidget *AllNetScanWidget::GetScanListWidget()
{
    return ui->ScanlistWidget;
}

void AllNetScanWidget::on_AllNetScanBtn_clicked()
{
    QJsonObject Body;
    QString ScanText=ui->lineEdit->text();
    Body["SCANUSER"]=ScanText;

    qDebug()<<__FILE__<<__LINE__<<Body;
    emit Sig_AllNetFindAll(MSG_TYPE_ALLNET_SCAN_ALL_REQUEST,Body);

}

void AllNetScanWidget::on_ScanlistWidget_itemClicked(QListWidgetItem *item)
{
    FriendRequestWidget* FriendReqW=new FriendRequestWidget;
    FriendReqW->SetUserName(item->text());
    FriendReqW->show();
    FriendReqW->setAttribute(Qt::WA_DeleteOnClose, true);
}

void AllNetScanWidget::ActForShowAllNetScanAll(QList<QPair<QString, QString> > recvData)
{
    GetScanListWidget()->clear();
    for(int i=0;i<recvData.size();i++){
        GetScanListWidget()->addItem(recvData[i].first);
    }
}

void AllNetScanWidget::LinkTcpSignalAndSlot()
{
    connect(MessageDispatcher::GetInstance()->getCsearchServer(),
            &CSearchService::EmitAllNetScanAll,
            this,&AllNetScanWidget::ActForShowAllNetScanAll);

    connect(this,&AllNetScanWidget::Sig_AllNetFindAll,
            TcpClient::get_instance(),&TcpClient::SendMsg);
}

